//将一个正整数拆分成若干个2的n次方的和
//target:目标数字 k:拆分次数
//10 2 =>  1010 => 2^3 + 2^1
function solution(target,k){

    let res = [];
    let binaryNum = target.toString(2);
    for (let i = 0; i < binaryNum.length; i++) {
        if(binaryNum[i] === '1'){
            res.push(binaryNum.length - 1 - i)
        }
    }

    if(res.length === k){
        return res;
    }else{
        if (res.length === 1 && res[0] >= 1 && k > 1){
            res = [res[0] - 1,res[0] - 1];
        }
        res = res.sort((a, b) => b - a);
        let res_0 = [...res];
        for (let i = 0; i < res.length; i++) {
            let jj = 0;
            for (let j = 0; j < res_0.length; j++) {
                if (res_0[j] === res[i]){
                    jj = j;
                    break;
                }
            }

            res_0 = [...res_0.slice(0, jj), ...res_0.slice(jj + 1)].concat(solution(Math.pow(2,res[i]),k - res.length + 1));
            if (res_0.length === k){
                return res_0;
            }
        }
        if (res_0.length === 0){
            return -1;
        }
    }

}


console.log(solution(10,3))